package com.flatfish.acvsystem.uitl;

import java.io.File;
import java.io.IOException;

import net.dongliu.apk.parser.ApkParser;
import net.dongliu.apk.parser.bean.ApkMeta;

import com.flatfish.acvsystem.vo.ACVApkAuthVO;
import com.flatfish.acvsystem.vo.ACVStandardApkVO;

@SuppressWarnings("resource")
public class ApkUtils {
	/**
	 * @author  luohuida
	 * @time    Mar 7, 2015
	 * @Description <b>解析出ManifestXml,以字符串形式返回.</b>
	 */
	public static String getManifestXml(String apkPath) {
		if(null==apkPath) return null;
		
		String xml = null;
		try {
			ApkParser apkParse = new ApkParser(new File(apkPath));
			xml = apkParse.getManifestXml();
		} catch (IOException e) {
			e.printStackTrace();
		}	
		return xml;
	}
	
	/**
	 * @author  luohuida
	 * @time    Mar 7, 2015
	 * @param apkPath
	 * @return
	 * @Description <b>以map形式返回</b>
	 */
	public static ACVApkAuthVO getApkMetaInfo(String apkPath){
		if(null==apkPath) return null;
		if(!isApk(apkPath)) return null;
		
		ACVApkAuthVO acvApkAuthVO = new ACVApkAuthVO();
		try {
			ApkParser apkParse = new ApkParser(new File(apkPath));
			ApkMeta apkMeta = apkParse.getApkMeta();
			
			acvApkAuthVO.setVersion(apkMeta.getVersionName());
			acvApkAuthVO.setPackageId(apkMeta.getPackageName());
			acvApkAuthVO.setName(apkMeta.getName());
			acvApkAuthVO.setHash(SHAUtils.SHAFileByLocalPath(apkPath));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return acvApkAuthVO;
	}
	
	/**
	 * @author  luohuida
	 * @time    Mar 9, 2015
	 * @param apkPath
	 * @return
	 * @Description <b>判断文件是否为apk</b>
	 */
	public static boolean isApk(String apkPath) {
		apkPath.lastIndexOf("\\");
		return apkPath.substring(apkPath.lastIndexOf("\\")+1).contains("apk")?true:false;
	}
}
